<?php
require_once("db.php");
require_once("Inland.class.php"); //arg: Color, name, defending_unit($object = null)
require_once("Army.class.php"); //arg: nation, color, unitId

$qry = "SELECT * FROM army";
$stmt = $db_connect -> prepare($qry);
$stmt -> bind_result($id,$nation,$color,$unitid);
$stmt -> execute();

$content_army = array();

while($stmt -> fetch()){
	$c_row = array();
	$c_row['id'] = $id;
	$c_row['nation'] = $nation;
	$c_row['color'] = $color;
	$c_row['unitid'] = $unitid;

	$content_army[] = $c_row;
}
$stmt -> close();
echo "<h2>Attacker:</H2>";
echo "<form action='' method='post' name='attack'>";
echo "Choose an army to attack with<br/><SELECT name='army'>";
echo "<option value='0'>None</option>";
foreach($content_army as $cont):
	echo "<option value=" . $cont['id'] . ">" .
			$cont['nation'] . " " . $cont['color'] . " " . $cont['unitid'] . "</option>";
endforeach;
echo "</SELECT>";
echo "<h2>Defenders:</H2>";
echo "<form action='' method='post' name='defence'>";
echo "Choose an army to defend with<br/><SELECT name='defence'>";
echo "<option value='0'>None</option>";
foreach($content_army as $cont):
	echo "<option value=" . $cont['id'] . ">" .
			$cont['nation'] . " " . $cont['color'] . " " . $cont['unitid'] . "</option>";
endforeach;
echo "</SELECT>";
echo "<h2>Support attacker:</H2>";
echo "<form action='' method='post' name='atksup'>";
echo "Choose an army to support attack with<br/><SELECT name='atksup'>";
echo "<option value='0'>None</option>";
foreach($content_army as $cont):
	echo "<option value=" . $cont['id'] . ">" .
			$cont['nation'] . " " . $cont['color'] . " " . $cont['unitid'] . "</option>";
endforeach;
echo "</SELECT>";
echo "<h2>Support defender:</H2>";
echo "<form action='' method='post' name='defsup'>";
echo "Choose an army to support attack with<br/><SELECT name='defsup'>";
echo "<option value='0'>None</option>";
foreach($content_army as $cont):
	echo "<option value=" . $cont['id'] . ">" .
			$cont['nation'] . " " . $cont['color'] . " " . $cont['unitid'] . "</option>";
endforeach;
echo "</SELECT>";

$qryp = "SELECT * FROM province";
$stmtp = $db_connect -> prepare($qryp);
$stmtp -> bind_result($idp,$colorp, $namep);
$stmtp -> execute();

$content_prov = array();

while($stmtp -> fetch()){
	$c_row = array();
	$c_row['idp'] = $idp;
	$c_row['colorp'] = $colorp;
	$c_row['namep'] = $namep;

	$content_prov[] = $c_row;
}

$stmtp -> close();
echo "<h2>Provinces:</H2>";
echo "Choose a province to attack:<br/>";
echo "<SELECT name='province'>";
foreach($content_prov as $contp):
	echo "<option value=" . $contp['idp'] . ">" .
			$contp['colorp'] . " " . $contp['namep'] . "</option>";
endforeach;
echo "</SELECT><br/><br/>";
echo "<input type='submit' value='ATTACK!'>";
echo "</form>";

//	echo $idatk . "<br/>";
//	echo $nationatk . "<br/>";
//	echo $coloratk . "<br/>";
//	echo $unitidatk . "<br/>";
if(isset($_POST['province'])){
	$provid = $_POST['province'];

	$qryprov = "SELECT * FROM province WHERE id = ?";

	$stmtprov = $db_connect -> prepare($qryprov);
	$stmtprov -> bind_param('i',$provid);
	$stmtprov -> execute();
	$stmtprov -> bind_result($idprov,$colorprov,$nameprov);
	$stmtprov-> fetch();

	$stmtprov -> close();
	$prov = new Inland($colorprov,$nameprov);
}
if(isset($_POST['army']) && $_POST['army'] != 0){
	$atkid = $_POST['army'];
	
	$qryatk = "SELECT * FROM army WHERE id = ?";

	$stmtatk = $db_connect -> prepare($qryatk);
	$stmtatk -> bind_param('i',$atkid);
	$stmtatk -> execute();
	$stmtatk -> bind_result($idatk,$nationatk,$coloratk,$unitidatk);
	$stmtatk -> fetch();

	$stmtatk -> close();
	$atkunit = new Army($nationatk,$coloratk,$unitidatk);
	$atkunit->moveUnit($prov);
}
if(isset($_POST['defence']) && $_POST['defence'] != 0){
	$defid = $_POST['defence'];

	$qrydef = "SELECT * FROM army WHERE id = ?";

	$stmtdef = $db_connect -> prepare($qrydef);
	$stmtdef -> bind_param('i',$defid);
	$stmtdef-> execute();
	$stmtdef -> bind_result($iddef,$nationdef,$colordef,$unitiddef);
	$stmtdef -> fetch();
	$stmtdef -> close();
	$defunit = new Army($nationdef,$colordef,$unitiddef);
	$prov->setDefending_unit($defunit);
}
if(isset($_POST['atksup']) && $_POST['atksup'] != 0){
	$atksupid = $_POST['atksup'];

	$qryatksup = "SELECT * FROM army WHERE id = ?";

	$stmtatksup = $db_connect -> prepare($qryatksup);
	$stmtatksup -> bind_param('i',$atksupid);
	$stmtatksup-> execute();
	$stmtatksup -> bind_result($idatksup,$nationatksup,$coloratksup,$unitidatksup);
	$stmtatksup -> fetch();
	$stmtatksup -> close();
	$atksupunit = new Army($nationatksup,$coloratksup,$unitidatksup);
	$prov->setAttacking_support_unit($atksupunit);
}
if(isset($_POST['defsup']) && $_POST['defsup'] != 0){
	$defsupid = $_POST['defsup'];

	$qrydefsup = "SELECT * FROM army WHERE id = ?";

	$stmtdefsup = $db_connect -> prepare($qrydefsup);
	$stmtdefsup -> bind_param('i',$defsupid);
	$stmtdefsup -> execute();
	$stmtdefsup -> bind_result($iddefsup,$nationdefsup,$colordefsup,$unitiddefsup);
	$stmtdefsup -> fetch();
	$stmtdefsup -> close();
	$defsupunit = new Army($nationdefsup,$colordefsup,$unitiddefsup);
	$prov->setDefending_support_unit($defsupunit);
}
if(isset($prov))
	$prov->resolve();
?>